package org.gruposp2p.dnie.server.rest;

import java.util.Collection;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gruposp2p.dnie.model.DNIeDocumentToSign;
import org.gruposp2p.dnie.service.DNIeDocumentToSignDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author jj
 */
@Path(value = "/mostSignedDocuments")
@Component
@Scope("request")
public class MostSignedDocumentsResource {

    private static Logger logger =
            LoggerFactory.getLogger(MostSignedDocumentsResource.class);

    @Autowired
    private DNIeDocumentToSignDAO documentToSignDao;

    @Context ServletContext servletContext;
    @Context HttpServletRequest request;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(
                @QueryParam("firstResult") @DefaultValue("0") int firstResult,
                @QueryParam("numberResults") @DefaultValue("15") int numberResults) {
        List<DNIeDocumentToSign> docs = null;
        try {
            docs = documentToSignDao.findOrderedByNumberSignaturesList(
                    firstResult, numberResults);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
    	}
        GenericEntity<Collection<DNIeDocumentToSign>> ge =
                new GenericEntity<Collection<DNIeDocumentToSign>>(docs) {};
        return Response.ok().entity(ge).build();
    }

}
